package org.example.dao.imp;

import org.example.dao.inter.TeacherDao;
import org.example.entitys.systems.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/13
 */

@Repository
public class TeacherDaoImp implements TeacherDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Teacher teacher) {
        String sql = "insert into t_manager_teacher(`teacher_name`,`subject`,`profession`," +
                "`school`,`introduce`,`login_id`) values ( ?, ?, ?, ?, ?, ? )";
        return jdbcTemplate.update(sql,
                teacher.getTeacher_name(),
                teacher.getSubject(),
                teacher.getProfession(),
                teacher.getSchool(),
                teacher.getIntroduce(),
                teacher.getLogin_id()
        );
    }

    @Override
    public int update(Teacher teacher) {
        String sql = "update t_manager_teacher set teacher_name = ?, subject = ?, profession = ?," +
                "school = ?, introduce = ?, login_id = ? where id = ? ";
        return jdbcTemplate.update(sql,
                teacher.getTeacher_name(),
                teacher.getSubject(),
                teacher.getProfession(),
                teacher.getSchool(),
                teacher.getIntroduce(),
                teacher.getLogin_id(),
                teacher.getId()
        );
    }

    @Override
    public int delete(int id) {
        String sql = "delete from t_manager_teacher where id = ? ";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public boolean isExitById(int id) {
        String sql = "select count(*) from t_manager_teacher where id = ? ";
        return jdbcTemplate.queryForObject(sql, Integer.class, id) > 0;
    }

    @Override
    public Teacher queryById(int id) {
        String sql = "select id, teacher_name, subject, profession, school, introduce, login_id" +
                " from t_manager_teacher where id = ? ";
        return jdbcTemplate.queryForObject(sql, this::transform, id);
    }

    @Override
    public List<Teacher> queryTeacherByName(String name) {
        String sql = "select id, teacher_name, subject, profession, school, introduce, login_id" +
                " from t_manager_teacher where teacher_name = ? ";
        return jdbcTemplate.query(sql, this::transform, name);
    }

    @Override
    public int queryLastInsertID() {
        String sql = "select LAST_INSERT_ID()";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public List<Teacher> queryAll() {
        String sql = "select id, teacher_name, subject, profession, school, introduce, login_id" +
                " from t_manager_teacher ";
        return jdbcTemplate.query(sql, this::transform);
    }

    @Override
    public Teacher transform(ResultSet rs, int RowNum) throws SQLException {
        return new Teacher(
                rs.getInt("id"),
                rs.getString("teacher_name"),
                rs.getString("subject"),
                rs.getString("profession"),
                rs.getString("school"),
                rs.getString("introduce"),
                rs.getInt("login_id")
        );
    }
}
